<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <!-- 引入 layui.css -->
    <link href="//unpkg.com/layui@2.8.0/dist/css/layui.css" rel="stylesheet">
    <!-- 引入 layui.js -->
    <script src="//unpkg.com/layui@2.8.0/dist/layui.js"></script>

    <style>
        #search{
            margin-top: 30px;
            margin-left: 1100px;
        }
    </style>
</head>
<body>
<table class="layui-hide" id="dataTable" lay-filter="dataTable"></table>

<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script type="text/javascript">
    layui.use('table', function () {
        var table = layui.table;
        // 创建渲染实例
        table.render({
            elem: '#dataTable',
            height: 600,
            url: 'warehouse/getSubMaterialViewAll', //数据接口
            page: true, //开启分页
            limit: 12,
            limits: [12],
            toolbar: '#toolbarDemo', // 工具栏选择器
            cols: [
                [
                    {field: 'id',title: 'ID',width: 80},
                    {field: 'brand', title: '品牌'},
                    {field: 'styleName', title: '款式名称'},
                    {field: 'materialsStatus', title: '物料状态',templet:function(b){
                            if (b.materialsStatus == "0") {
                                return "面里料";
                            } else if (b.materialsStatus == "1") {
                                return "辅料";
                            }else if (b.materialsStatus == "2") {
                                return "包装辅料";
                            }
                        }},
                    {field: 'materialsName', title: '物料名'},
                    {field: 'supplier', title: '供应商'},
                    {field: 'styleNumber', title: '款号'},
                    {field: 'quotationNumber', title: '订单号'},
                    {field: 'productName', title: '商品名称'},
                    {field: 'takeAwayCount', title: '拿货数量'},
                    {field: 'takeAwaySubmitTime', title: '申请提交时间'},
                    // {field: 'takeAwayTime', title: '拿货时间'},
                    {field: 'src', title: '备注'},
                    {field: 'takeAwayStatus', title: '申请状态',templet:function(b){
                            if(b.takeAwayStatus == "0"){
                                return "未审核";
                            }else if (b.takeAwayStatus == "1") {
                                return "已提交审核";
                            } else if (b.takeAwayStatus == "2") {
                                return "申请成功";
                            }else if (b.takeAwayStatus == "3") {
                                return "申请不成";
                            }
                        }},
                    {field: '', title: '操作', width: 140, templet:function(b){
                            if(b.takeAwayStatus == "0"){
                                return '<button class="layui-btn layui-btn-normal layui-btn-sm" id="audit"  lay-event="audit">审核</button>';
                            }else if (b.takeAwayStatus == "1") {
                                return '<button class="layui-btn layui-btn-normal layui-btn-sm layui-btn-disabled" id="audit"  lay-event="audit">已提交审核</button>';
                            } else if (b.takeAwayStatus == "2") {
                                return '<button class="layui-btn layui-btn-normal layui-btn-sm layui-btn-disabled" id="audit"  lay-event="audit">申请成功</button>';
                            }else if (b.takeAwayStatus == "3") {
                                return '<button class="layui-btn layui-btn-danger layui-btn-sm" id="audit"  lay-event="audit">再次申请</button>';
                            }
                        }}
                ]
            ]
        });

    });
    layui.use('table', function() {
        var table = layui.table;
        // 监听表格工具栏按钮的点击事件
        table.on('tool(dataTable)', function(obj) {
            var data = obj.data; // 获得当前行数据
            let takeAwayStatus= data.takeAwayStatus;
            if (takeAwayStatus == 1 || takeAwayStatus == 2){
                return false;
            }
            if (obj.event === 'audit') {
                var id = data.id; // 提取订单ID
                console.log(id)
                $.ajax({
                    url: 'warehouse/updatelMaterials',
                    type: 'get',
                    data: {id:id},
                    dataType: 'text',
                    // contentType: 'application/json;charset=utf-8',
                    success: function(result) {
                        console.log(result)
                        if (result == 1) {
                            // localStorage.setItem(id, '已提交审核');
                            // // 修改按钮文本为"已提交审核"
                            // var btnElem = $(obj.tr).find('#audit');
                            // btnElem.text('已提交审核');
                            // btnElem.removeClass('layui-btn-danger').addClass('layui-btn-disabled');
                            // btnElem.attr('disabled', 'disabled');
                            layer.msg("已提交审核")
                        } else {
                            alert('修改失败，服务器返回：' + result);
                        }
                        table.reload('dataTable')
                    },
                    error: function(jqXHR, textStatus, errorThrown) {
                        alert('请求失败，错误信息：' + errorThrown);
                    }
                });
            }
        });

    });

</script>

<script type="text/html" id="barDemo">
    <div class="layui-btn-container">
        <a class="layui-btn layui-btn-normal layui-btn-sm" id="audit" lay-event="audit">审核</a>
    </div>
</script>
</body>
</html>
